package com.fxd.shopping.product.app;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fxd.model.product.CategoryEntity;
import com.fxd.shopping.product.service.CategoryService;
import com.fxd.core.util.RR;


/**
 * 商品三级分类
 *
 * @author fuxdong
 * @email 2016335176@qqcom
 * @date 2021-07-17 10:30:51
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public RR list() {
        List<CategoryEntity> list = categoryService.listWithTree();

        return RR.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public RR info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return RR.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RR save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return RR.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RR update(@RequestBody CategoryEntity category) {
        categoryService.updateCascade(category);

        return RR.ok();
    }

    /**
     * 批量修改
     */
    @RequestMapping("/update/sort")
    public RR updateSort(@RequestBody CategoryEntity[] category) {
        categoryService.updateBatchById(Arrays.asList(category));

        return RR.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RR delete(@RequestBody Long[] catIds) {

        //1，检查当前删除的菜单，是否被别的地方引用
        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return RR.ok();
    }

}
